package com.mobilous.client.project.pagemanager;

import org.vaadin.gwtgraphics.client.DrawingArea;
import org.vaadin.gwtgraphics.client.Line;

import com.google.gwt.touch.client.Point;
import com.google.gwt.user.client.ui.Widget;

public class LineLink {
	private Widget source;
	private Widget target;
	private DrawingArea frame;
	public Line lineDraw;
	
	public LineLink(DrawingArea frame)
	{
		this.frame=frame;
	}
	
	public LineLink(Widget source,Widget target,DrawingArea frame) {
		this.source=source;
		this.target=target;
		this.frame=frame;
	}
	
	
	public void draw()
	{
		int x = source.getAbsoluteLeft() - frame.getAbsoluteLeft();
		int y = source.getAbsoluteTop() - frame.getAbsoluteTop();
		
		int sourcewidth=source.getOffsetWidth();
		int sourceheight=source.getOffsetHeight();
		
		Point sourcetop=new Point(sourcewidth/2,0);
		Point sourceleft=new Point(0,sourceheight/2);
		Point sourcebottom = new Point(sourcewidth/2,sourceheight);
		Point sourceright = new Point(sourcewidth,sourceheight/2);
		
		int targetwidth=target.getOffsetWidth();
		int targetheight=target.getOffsetHeight();
		
		Point targettop=new Point(targetwidth/2,0);
		Point targetleft=new Point(0,targetheight/2);
		Point targetbottom = new Point(targetwidth/2,targetheight);
		Point targetright = new Point(targetwidth,targetheight/2);
		
		//all opoints
		Point obj[] = new Point[4];
		
		obj[0] = new Point(x+sourcetop.getX(),y+sourcetop.getY());//top
		obj[1] = new Point(x+sourceleft.getX(),y+sourceleft.getY());//left
		obj[2] = new Point(x+sourceright.getX(),y+sourceright.getY());//right
		obj[3] = new Point(x+sourcebottom.getX(),y+sourcebottom.getY());//bottom

		
		int x1 = target.getAbsoluteLeft() - frame.getAbsoluteLeft();
		int y1 = target.getAbsoluteTop() - frame.getAbsoluteTop();
		
		//all opoints
		Point obj1[] = new Point[4];
		obj1[0] = new Point(x1+targettop.getX(),y1+targettop.getY());//top
		obj1[1] = new Point(x1+targetleft.getX(),y1+targetleft.getY());//left
		obj1[2] = new Point(x1+targetright.getX(),y1+targetright.getY());//right
		obj1[3] = new Point(x1+targetbottom.getX(),y1+targetbottom.getY());//bottom

		
		LineTr[] line = new LineTr[16];
		
		line[0] =  new LineTr(obj[0],obj1[0]);
		line[1] =  new LineTr(obj[0],obj1[1]);
		line[2] =  new LineTr(obj[0],obj1[2]);
		line[3] =  new LineTr(obj[0],obj1[3]);
		line[4] =  new LineTr(obj[1],obj1[0]);
		line[5] =  new LineTr(obj[1],obj1[1]);
		line[6] =  new LineTr(obj[1],obj1[2]);
		line[7] =  new LineTr(obj[1],obj1[3]);
		line[8] =  new LineTr(obj[2],obj1[0]);
		line[9] =  new LineTr(obj[2],obj1[1]);
		line[10] = new LineTr(obj[2],obj1[2]);
		line[11] = new LineTr(obj[2],obj1[3]);
		line[12] = new LineTr(obj[3],obj1[0]);
		line[13] = new LineTr(obj[3],obj1[1]);
		line[14] = new LineTr(obj[3],obj1[2]);
		line[15] = new LineTr(obj[3],obj1[3]);
		
		LineTr smallest=line[0];
		for(int i=0;i<16;i++)
		{
			if(smallest.length()>line[i].length())
			{
				smallest=line[i];
			}
		}
		//Line
		lineDraw = new Line((int)smallest.x,(int)smallest.y,(int)smallest.x1,(int)smallest.y1);
		lineDraw.setStrokeWidth(1);
		lineDraw.setStrokeOpacity(.5);
		frame.add(lineDraw);
	}
}
